package com.jojo.android.zxlib.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jojo.android.zxlib.R;


/**
 * 创建者 zx
 * 创建时间 2021/1/23 13:31
 */
public class EmptyView extends FrameLayout implements View.OnClickListener {
    private final String TAG = "EmptyView";
    MiniLoadingView miniLoadingView;
    TextView emptyTipsTv;
    TextView emptyReloadTv;
    LinearLayout emptyTipsLayout;

    private OnReloadClickListener onReloadClickListener;

    public EmptyView(@NonNull Context context) {
        this(context, null);
    }

    public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.layout_emptyview, this);
        miniLoadingView = findViewById(R.id.mini_loading_view);
        emptyTipsTv = findViewById(R.id.empty_tips_tv);
        emptyReloadTv = findViewById(R.id.empty_reload_tv);
        emptyTipsLayout = findViewById(R.id.empty_tips_layout);
        emptyReloadTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == emptyReloadTv){
            if (onReloadClickListener!=null){
                onReloadClickListener.onReloadClick(this);
            }
        }
    }

    /**
     * 显示加载中
     */
    public void showLoading() {
        miniLoadingView.setVisibility(VISIBLE);
        emptyTipsLayout.setVisibility(GONE);
    }

    /**
     * 显示提示信息
     *
     * @param tips
     */
    public void showTips(CharSequence tips) {
        emptyTipsLayout.setVisibility(VISIBLE);
        emptyTipsTv.setVisibility(VISIBLE);
        emptyTipsTv.setText(tips);
        emptyReloadTv.setVisibility(GONE);
        miniLoadingView.setVisibility(GONE);
    }

    /**
     * 显示错误信息
     *
     * @param err    错误信息
     * @param reload 是否显示重试按钮
     */
    public void showFail(CharSequence err, boolean reload) {
        emptyTipsLayout.setVisibility(VISIBLE);
        miniLoadingView.setVisibility(GONE);
        emptyTipsTv.setVisibility(VISIBLE);
        emptyTipsTv.setText(err);
        if (reload) {
            emptyReloadTv.setVisibility(VISIBLE);
        } else {
            emptyReloadTv.setVisibility(GONE);
        }
    }

    public void setOnReloadClickListener(OnReloadClickListener onReloadClickListener) {
        this.onReloadClickListener = onReloadClickListener;
    }

    public interface OnReloadClickListener {
        void onReloadClick(EmptyView emptyView);
    }

    public TextView getEmptyTipsTv() {
        return emptyTipsTv;
    }

    public TextView getEmptyReloadTv() {
        return emptyReloadTv;
    }
}
